<?php



namespace app\index\controller;

use think\captcha\Captcha;
use think\Validate;

class Balance extends Base
{
    /**
     * 验证码
     *
     *
     * @return \think\Response
     */
    public function vercode()
    {
        $config =    [
            // 验证码字体大小
            'fontSize'    =>    14,
            // 验证码位数
            'length'      =>    4,
            // 关闭验证码杂点
            'useNoise'    =>    false,
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }

    /**
     * 资金详情与变动记录
     *
     *
     * @return mixed
     */
    public function index()
    {
        $where = ['uid' => is_login()];

        //详情
        $this->common($where);

        //变动记录
        $this->assign('list', $this->logicBalanceChange->getBalanceChangeList($where, true, 'id desc', 10));

        return $this->fetch();
    }

    /**
     * 收款账户
     *
     *
     * @return mixed
     */
    public function account()
    {
        $where = ['a.uid' => is_login()];
        //详情
        $this->common();

        //组合搜索
        !empty($this->request->get('banker')) && $where['bank_id']
            = ['eq', $this->request->get('banker')];

        //状态
        $where['a.status'] = ['eq', $this->request->get('status', '')];
        //收款账户
        $this->assign('list', $this->logicUserAccount->getAccountList($where, 'a.*,b.id as b_id,b.name as banker', 'create_time desc'));

        return $this->fetch();
    }

    /**
     * 新增
     *
     *
     * @return mixed
     */
    public function addAccount()
    {

        if ($this->request->isPost()) {
            if ($this->request->post('b/a')['uid'] == is_login()) {
                $this->result($this->logicUserAccount->saveUserAccount($this->request->post('b/a')));
            } else {
                $this->result(0, '非法操作，请重试！');
            }
        }
        //银行
        $this->assign('banker', $this->logicBanker->getBankerList());

        return $this->fetch();
    }

    /**
     * 新增
     *
     *
     * @return mixed
     */
    public function editAccount()
    {

        if ($this->request->isPost()) {
            if ($this->request->post('b/a')['uid'] == is_login()) {
                $this->result($this->logicUserAccount->saveUserAccount($this->request->post('b/a')));
            } else {
                $this->result(0, '非法操作，请重试！');
            }
        }
        //银行
        $this->assign('banker', $this->logicBanker->getBankerList());

        //详情信息
        $this->assign('info', $this->logicUserAccount->getAccountInfo(['uid' => is_login(), 'id' => $this->request->param('id')]));

        return $this->fetch();
    }

    /**
     * 打款记录
     *
     *
     * @return mixed
     */
    public function paid()
    {
        $where = ['a.uid' => is_login()];

        //时间搜索  时间戳搜素
        $where['a.create_time'] = $this->parseRequestDate();

        //列表
        $this->assign('list', $this->logicBalanceCash->getOrderCashList($where));

        return $this->fetch();
    }

    /**
     * 自主提现申请
     *
     *
     * @return mixed
     */
    public function apply()
    {
        $where = ['uid' => is_login()];

        if ($this->request->isPost()) {
            if ($this->request->post('b/a')['uid'] == is_login()) {
                $this->result($this->logicBalanceCash->saveUserCashApply($this->request->post('b/a')));
            } else {
                $this->result(0, '非法操作，请重试！');
            }
        }
        //详情
        $this->common($where);
        //收款账户
        $this->assign('list', $this->logicUserAccount->getAccountList(['a.uid' => is_login(), 'a.status' => 1], 'a.*,b.id as b_id,b.name as banker', 'a.create_time desc'));

        return $this->fetch();
    }

    /**
     * Common
     *
     *
     * @param array $where
     */
    public function common($where = [])
    {
        //资产信息
        $this->assign('info', $this->logicBalance->getBalanceInfo($where));
        //银行
        //$this->assign('banker', $this->logicBanker->getBankerList());
    }

    /**
     * 充值
     */
    public function charge()
    {
        if ($this->request->isPost()) {
            return $this->chargeForm($this->request->post('b/a')['amount'] * 100, config('code.fuiou')['chargeNotifyUrl'] . is_login(), '17091929413');
        }

        $where = ['uid' => is_login()];
        $this->common($where);

        $where2 = ['uid' => $where['uid'], 'type' => 1];
        $this->assign('logList', $this->logicProxyBatch->getProxyOrderList($where2, true, 'id desc', 10));

        return $this->fetch();
    }

    public function chargeForm($amt, $url, $mobile)
    {

        $cd = config('code.fuiou')['cd'];
        $key = config('code.fuiou')['key'];
        $chargeFormUrl = config("code.fuiou")['chargeFormUrl'];

        $params = [
            'amt' => $amt,
            'back_notify_url' => $url,
            'in_cust_no' => $mobile,
            'mchnt_cd' => $cd,
            'mchnt_txn_dt' => date("Ymd"),
            'mchnt_txn_ssn' => date("YmdHis"),
            'order_pay_type' => 'B2O',
            'page_notify_url' => '',
            'rem' => '',
            'sys_cd' => 'WBXT',
            'tran_name' => 'XXMCZ',
        ];

        $sigSrc = implode("|", $params) . "|" . $key;
        $params['signature'] = md5($sigSrc);

        echo <<<HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 
<h2>加载中...</h2>
<form method="post" action="{$chargeFormUrl}" id="charge-form">

HTML;
        foreach ($params as $key => $value) {
            echo "<input type=\"text\" name=\"{$key}\" value=\"{$value}\" hidden=\"true\" />\n";
        }

        echo <<<HTML
        <!--<input type="submit" />-->
</form>
<script>
  window.onload=function(){
      document.getElementById("charge-form").submit();
}
</script>
</body>
</html>
HTML;
    }

    /**
     * 提现
     * @return mixed|void
     */
    public function withdraw()
    {
        $user = $this->logicUser->getUserInfo(['uid' => is_login()], 'cash_password, cash_channel');
        if (empty($user["cash_password"])) {
            $this->redirect(url('index/User/cashpwd'));
        } else {
            if ($this->request->isPost()) {
                $data = $this->request->post();
                $validate = $this->validateWithDraw->check($data);
                if (!$validate) {
                    $this->result(0, $this->validateWithDraw->getError());
                } else {
                    $cashpwd = $data["cashpwd"];
                    if ($user["cash_password"] != data_md5_key($cashpwd)) {
                        $this->result(0, "提现密码错误。");
                    } else {
                        $file = $this->logicFile->fileUpload('excel', 'userauth/' . is_login());
                        $remark = $data['remark'];
                        $result = $this->logicBalanceCash->saveUserCashApplyBatch(UPLOAD_PATH . $file['data']['src'], $remark);
                        $this->result($result[0], $result[1]);
                    }
                }
            }

            $where1 = ['uid' => is_login()];
            $this->common($where1);

            $where2 = ['a.uid' => is_login()];
            $this->assign('list', $this->logicBalanceCash->getOrderCashList($where2, 'a.*,u.account,b.name as method', 'a.update_time desc', 10));
            $channel = $this->logicPay->getChannelInfo(['id' => $user['cash_channel']], 'action');

            $this->assign("fileName", $channel['action'] == "Fastpay" ? "批量转账模版2" : "批量转账模版");
            

            return $this->fetch();
        }
    }

    /**
     * 结算
     * @return mixed|void
     */
    public function settlement()
    {
        $user = $this->logicUser->getUserInfo(['uid' => is_login()], 'cash_password, cash_channel');
        if (empty($user["cash_password"])) {
            $this->redirect(url('index/User/cashpwd'));
        } else {
            if ($this->request->isPost()) {
                $data = $this->request->post();
                // $validate = $this->validateWithDraw->check($data);
                // if (!$validate) {
                //     $this->result(0, $this->validateWithDraw->getError());
                // } else {
                //     $cashpwd = $data["cashpwd"];
                //     if ($user["cash_password"] != data_md5_key($cashpwd)) {
                //         $this->result(0, "提现密码错误。");
                //     } else {
                        $file = $this->logicFile->fileUpload('excel', 'userauth/' . is_login());
                        $result = $this->logicBalanceSettlement->saveUserSettlementApplyBatch(UPLOAD_PATH . $file['data']['src']);
                        $this->result($result[0], $result[1]);
                   // }
                //}
            }

            $where1 = ['uid' => is_login()];
            $this->common($where1);

            $where2 = ['a.uid' => is_login()];
            $this->assign('list', $this->logicBalanceSettlement->getSettlementList($where2, 'a.*', 'a.update_time desc', 10));

            $this->assign("fileName", "批量结算模版");

            return $this->fetch();
        }
    }
}
